#include "can.h"
#include "delay.h"
#include "display_state.h"
#include "display.h"
#include "display_alarm_set.h"


CanRxMsg RxMessage;	
extern uint8_t activation_page_num;	
extern u8 Page_Cell_Temp_id;

//////////////////////////////////////////////////////////////////////////////////
//CAN初始化
//tsjw:重新同步跳跃时间单元.范围:1~3; CAN_SJW_1tq	 CAN_SJW_2tq CAN_SJW_3tq CAN_SJW_4tq
//tbs2:时间段2的时间单元.范围:1~8;
//tbs1:时间段1的时间单元.范围:1~16;	  CAN_BS1_1tq ~CAN_BS1_16tq
//brp :波特率分频器.范围:1~1024;(实际要加1,也就是1~1024) tq=(brp)*tpclk1
//注意以上参数任何一个都不能设为0,否则会乱.
//波特率=Fpclk1/((tsjw+tbs1+tbs2)*brp);
//mode:0,普通模式;1,回环模式;
//Fpclk1的时钟在初始化的时候设置为36M,如果设置CAN_Normal_Init(1,8,7,5,1);
//则波特率为:36M/((1+8+7)*5)=450Kbps
//返回值:0,初始化OK;
//    其他,初始化失败
u8 CAN_Mode_Init(u8 tsjw,u8 tbs2,u8 tbs1,u16 brp,u8 mode)
{

	GPIO_InitTypeDef       GPIO_InitStructure; 
	CAN_InitTypeDef        CAN_InitStructure;
 	CAN_FilterInitTypeDef  CAN_FilterInitStructure;
	NVIC_InitTypeDef       NVIC_InitStructure;

	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_AFIO, ENABLE);//使能PORTB时钟	                   											 
  RCC_APB1PeriphClockCmd(RCC_APB1Periph_CAN1, ENABLE);                     //使能CAN1时钟	
	
	//GPIO_PinRemapConfig(GPIO_Remap1_CAN1,ENABLE);

  GPIO_InitStructure.GPIO_Pin   = GPIO_Pin_12;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  GPIO_InitStructure.GPIO_Mode  = GPIO_Mode_AF_PP;     //复用推挽
  GPIO_Init(GPIOA, &GPIO_InitStructure);		           //初始化IO
   
  GPIO_InitStructure.GPIO_Pin   = GPIO_Pin_11;
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  GPIO_InitStructure.GPIO_Mode  = GPIO_Mode_IPU;       //上拉输入
  GPIO_Init(GPIOA, &GPIO_InitStructure);               //初始化IO
	  
 	//CAN单元设置
 	CAN_InitStructure.CAN_TTCM=DISABLE;						 //非时间触发通信模式  
 	CAN_InitStructure.CAN_ABOM=DISABLE;						 //软件自动离线管理	
  CAN_InitStructure.CAN_AWUM=DISABLE;						 //睡眠模式通过软件唤醒(清除CAN->MCR的SLEEP位)
  CAN_InitStructure.CAN_NART=ENABLE;						 //禁止报文自动传送 
  CAN_InitStructure.CAN_RFLM=DISABLE;						 //报文不锁定,新的覆盖旧的 
  CAN_InitStructure.CAN_TXFP=DISABLE;						 //优先级由报文标识符决定 
  CAN_InitStructure.CAN_Mode= mode;	                     //模式设置： mode:0,普通模式;1,回环模式; 
  //设置波特率
  CAN_InitStructure.CAN_SJW=tsjw;				                   //重新同步跳跃宽度(Tsjw)为tsjw+1个时间单位  CAN_SJW_1tq	 CAN_SJW_2tq CAN_SJW_3tq CAN_SJW_4tq
  CAN_InitStructure.CAN_BS1=tbs1;                          //Tbs1=tbs1+1个时间单位CAN_BS1_1tq ~CAN_BS1_16tq
  CAN_InitStructure.CAN_BS2=tbs2;                          //Tbs2=tbs2+1个时间单位CAN_BS2_1tq ~	CAN_BS2_8tq
  CAN_InitStructure.CAN_Prescaler=brp;                     //分频系数(Fdiv)为brp+1	//
  CAN_Init(CAN1, &CAN_InitStructure);                      // 初始化CAN1 

 	CAN_FilterInitStructure.CAN_FilterNumber = 0;	                      //过滤器0
 	CAN_FilterInitStructure.CAN_FilterMode   = CAN_FilterMode_IdMask;   //屏蔽模式
  CAN_FilterInitStructure.CAN_FilterScale  = CAN_FilterScale_32bit;   //32位 
	
  CAN_FilterInitStructure.CAN_FilterIdHigh     = 0xC586;              //32位ID
  CAN_FilterInitStructure.CAN_FilterIdLow      = 0x87A0;
  
	
  CAN_FilterInitStructure.CAN_FilterMaskIdHigh = 0xFF87;              //32位MASK
  CAN_FilterInitStructure.CAN_FilterMaskIdLow  = 0xFFF8;
  
//    CAN_FilterInitStructure.CAN_FilterIdHigh     = 0xF800;              //32位ID
//    CAN_FilterInitStructure.CAN_FilterIdLow      = 0x07F8;
//  
//	
//    CAN_FilterInitStructure.CAN_FilterMaskIdHigh = 0xC000;              //32位MASK
//    CAN_FilterInitStructure.CAN_FilterMaskIdLow  = 0x07A0;
  
  
//  CAN_FilterInitStructure.CAN_FilterIdHigh     = 0;              //32位ID
//  CAN_FilterInitStructure.CAN_FilterIdLow      = 0;
//  
//	
//  CAN_FilterInitStructure.CAN_FilterMaskIdHigh = 0;              //32位MASK
//  CAN_FilterInitStructure.CAN_FilterMaskIdLow  = 0;
	
	
  CAN_FilterInitStructure.CAN_FilterFIFOAssignment = CAN_Filter_FIFO0;//过滤器0关联到FIFO0
 	CAN_FilterInitStructure.CAN_FilterActivation = ENABLE;              //激活过滤器0

  CAN_FilterInit(&CAN_FilterInitStructure);//滤波器初始化

	CAN_ITConfig(CAN1,CAN_IT_FMP0,ENABLE);//FIFO0消息挂号中断允许.		    
  
  NVIC_InitStructure.NVIC_IRQChannel = USB_LP_CAN1_RX0_IRQn;
  NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;     // 主优先级为1
  NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;            // 次优先级为0
  NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  NVIC_Init(&NVIC_InitStructure);

	return 0;
}   
/*****************************************************
** @函数名：USART2_IRQHandler
** @描述  ：无
** @参数  ：无
** @返回值：串口2接收中断
*****************************************************/
u16 Vol_value[24];
u16 I_value;
u16 Obj_value;

u8  Cell_type_set = 0;
u8  JH_value_set = 0;
u16 I_value_set = 2000; //默认发送值
u16 Obj_value_set = 2400;//默认发送值
u32 SW_value_set = 0x00000000;
	






void USB_LP_CAN1_RX0_IRQHandler(void)
{
  u8 n;
  u8 index,i;
  u8 Frame_ID;

  CAN_Receive(CAN1,0, &RxMessage);                       //接收到的CAN数据
  Frame_ID = (RxMessage.ExtId >> 16) & 0x0F;

  if(Frame_ID == 1)
  {
	  sys_totle_v = RxMessage.Data[0]| (RxMessage.Data[1] << 8);
	  sys_totle_i = RxMessage.Data[2]| (RxMessage.Data[3] << 8);
	  max_tmp_data= RxMessage.Data[4];
	  min_tmp_data= RxMessage.Data[5];
	  sys_totle_c = RxMessage.Data[6];
	  if(sys_totle_c > 100)sys_totle_c = 100;
	  
	  sys_fault   = RxMessage.Data[7];
	  
	  //如果在要修改的值在当前页，则刷新
	  Refresh_Display_flag = 1;	  
  }
  else if(Frame_ID == 2)
  {
	  cell_num = RxMessage.Data[0];
	  temp_num = RxMessage.Data[1];
	  max_vol_data = RxMessage.Data[2]| (RxMessage.Data[3] << 8);
	  min_vol_data = RxMessage.Data[4]| (RxMessage.Data[5] << 8);
	  toucher_state =RxMessage.Data[6];
	  life_tick = RxMessage.Data[7];
	  
	  //如果在要修改的值在当前页，则刷新
	  Refresh_Display_flag = 1;	  
  }
  
  else if(Frame_ID == 3)
  {
	  max_vol_id = RxMessage.Data[0];
	  min_vol_id = RxMessage.Data[1];
	  max_tmp_id = RxMessage.Data[2];
	  min_tmp_id = RxMessage.Data[3];
	  insulation = RxMessage.Data[4]| (RxMessage.Data[5] << 8);
	  soh        = RxMessage.Data[6];
	  
	  temp2_num = (RxMessage.Data[7] >> 0) & 0x0F;
	  temp3_num = (RxMessage.Data[7] >> 4) & 0x0F;
	  
	  
	  //如果在要修改的值在当前页，则刷新
	  Refresh_Display_flag = 1;			  
  } 

  else if(Frame_ID == 4)
  {
	  n = RxMessage.Data[0];
	  if(n*3 + 2 < 250)
	  {
		  cell_vol[n*3 + 1] = RxMessage.Data[1]| (RxMessage.Data[2] << 8);
		  cell_vol[n*3 + 2] = RxMessage.Data[3]| (RxMessage.Data[4] << 8);
		  cell_vol[n*3 + 3] = RxMessage.Data[5]| (RxMessage.Data[6] << 8);		  
	  }
	  //如果在要修改的值在当前页，则刷新
	  if(activation_page_num == page_cell)
			Refresh_Display_flag = 1;	  	  
	  
  }	

  else if(Frame_ID == 5)
  {
	  n = RxMessage.Data[0];
	  if(n*6 + 5 < 250)
	  {
		  cell_tmp[n*6 + 1] = RxMessage.Data[1];
		  cell_tmp[n*6 + 2] = RxMessage.Data[2];
		  cell_tmp[n*6 + 3] = RxMessage.Data[3];
		  cell_tmp[n*6 + 4] = RxMessage.Data[4];
		  cell_tmp[n*6 + 5] = RxMessage.Data[5];
		  cell_tmp[n*6 + 6] = RxMessage.Data[6];	  
	  }	
	  //如果在要修改的值在当前页，则刷新
	  if(activation_page_num == page_temp)
			Refresh_Display_flag = 1;	 	  
  }	


  else if(Frame_ID == 6)
  {
	  n = RxMessage.Data[0];
	  if(n*6 + 5 < 250)
	  {
		  cell_tmp_in[n*6 + 1] = RxMessage.Data[1];
		  cell_tmp_in[n*6 + 2] = RxMessage.Data[2];
		  cell_tmp_in[n*6 + 3] = RxMessage.Data[3];
		  cell_tmp_in[n*6 + 4] = RxMessage.Data[4];
		  cell_tmp_in[n*6 + 5] = RxMessage.Data[5];
		  cell_tmp_in[n*6 + 6] = RxMessage.Data[6];	  
	  }	
	  //如果在要修改的值在当前页，则刷新
	  if(activation_page_num == page_temp)
			Refresh_Display_flag = 1;	 	  
  }	 
  
  
  else if(Frame_ID == 7)
  {
	  n = RxMessage.Data[0];
	  if(n*6 + 5 < 250)
	  {
		  cell_tmp_out[n*6 + 1] = RxMessage.Data[1];
		  cell_tmp_out[n*6 + 2] = RxMessage.Data[2];
		  cell_tmp_out[n*6 + 3] = RxMessage.Data[3];
		  cell_tmp_out[n*6 + 4] = RxMessage.Data[4];
		  cell_tmp_out[n*6 + 5] = RxMessage.Data[5];
		  cell_tmp_out[n*6 + 6] = RxMessage.Data[6];	  
	  }	
	  //如果在要修改的值在当前页，则刷新
	  if(activation_page_num == page_temp)
			Refresh_Display_flag = 1;	 	  
  }	 

  
  
}
//can发送一组数据(固定格式:ID为0X12,标准帧,数据帧)	
//len:数据长度(最大为8)				     
//msg:数据指针,最大为8个字节.
//返回值:0,成功;
//		 其他,失败;
u8 Can_Send_Msg(u32 ExtId,u8* msg,u8 len)
{	
  u8 mbox;
  u16 i=0;
	u32 ID = 0;
  CanTxMsg TxMessage;
	
	
  TxMessage.ExtId = ExtId;		   // 设置扩展标示符（29位）
  TxMessage.IDE   = CAN_ID_EXT;  // 使用扩展标识符
  TxMessage.RTR   = 0;		       // 消息类型为数据帧，一帧8位
  TxMessage.DLC   = len;		     // 发送数据帧长度
  for(i=0;i<len;i++)
  {
    TxMessage.Data[i]=msg[i];    // 第一帧信息       
  }		
  mbox= CAN_Transmit(CAN1, &TxMessage);   
  i=0;
  while((CAN_TransmitStatus(CAN1, mbox)!=CAN_TxStatus_Failed)&&(i<0XFFF))i++;	//等待发送结束
  if(i>=0XFFF)
  {
	  return 1;
  }
  return 0;		
}





u8 Can_Send_STD_Msg(u32 ExtId,u8* msg,u8 len)
{	
  u8 mbox;
  u16 i=0;
	u32 ID = 0;
  CanTxMsg TxMessage;
	
	
  TxMessage.StdId = ExtId;		   // 设置标准标示符（29位）
  TxMessage.IDE   = CAN_ID_STD;  // 使用标准帧
  TxMessage.RTR   = 0;		       // 消息类型为数据帧，一帧8位
  TxMessage.DLC   = len;		     // 发送数据帧长度
  for(i=0;i<len;i++)
  {
    TxMessage.Data[i]=msg[i];    // 第一帧信息       
  }		
  mbox= CAN_Transmit(CAN1, &TxMessage);   
  i=0;
  while((CAN_TransmitStatus(CAN1, mbox)!=CAN_TxStatus_Failed)&&(i<0XFFF))i++;	//等待发送结束
  if(i>=0XFFF)
  {
	  return 1;
  }
  return 0;		
}






